package chuishop.yiqimaicai.controller;

import chuishop.yiqimaicai.mapper.BusinessDetailsMapper;
import chuishop.yiqimaicai.pojo.BusinessDetails;
import chuishop.yiqimaicai.pojo.Typegoods;
import chuishop.yiqimaicai.service.BusinessDetailsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
//业务明细,商品进仓出仓情况
public class BusinessDetailsController {

    @Autowired
    private BusinessDetailsService businessDetailsService;

    @RequestMapping("findAllBusinessDetails")
    @ResponseBody
    public PageInfo<BusinessDetails> findAllBusinessDetails(Integer pageNo, Integer pageSize){
        if(pageNo==null){
            pageNo=1;
        }
        if(pageSize==null){
            pageSize=5;
        }
        //启动分页
        PageHelper.startPage(pageNo,pageSize);

        List<BusinessDetails> list=businessDetailsService.findAllBusinessDetails();
        for(int i=0;i<list.size();i++) {
            System.out.println(list.get(i).getBdid()+"\t"+list.get(i).getGid()+"\t"+list.get(i).getSid()
                    +"\t"+list.get(i).getAgnum()+"\t"+list.get(i).getOperation()+"\t"+list.get(i).getCreattime());
        }
        PageInfo<BusinessDetails> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }

    // 情况0：通过gid,sid,gsid值搜索
    @RequestMapping("findDetailsById")
    @ResponseBody
    public PageInfo<BusinessDetails> findDetailsById(Integer gid,Integer sid,Integer gsid,Integer pageNo, Integer pageSize){
        if(pageNo==null){
            pageNo=1;
        }
        if(pageSize==null){
            pageSize=5;
        }
        //启动分页
        PageHelper.startPage(pageNo,pageSize);

        List<BusinessDetails> list=businessDetailsService.findDetailsById(gid,sid,gsid);

        for(int i=0; i<list.size(); i++) {
            System.out.println(list.get(i).getBdid()+"\t"+list.get(i).getGid()+"\t"+list.get(i).getSid()
                    +"\t"+list.get(i).getAgnum()+"\t"+list.get(i).getOperation()+"\t"+list.get(i).getCreattime());
        }
        PageInfo<BusinessDetails> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }
    //情况1：通过gid值搜索
    @RequestMapping("findDetailsByGid")
    @ResponseBody
    public PageInfo<BusinessDetails> findDetailsByGid(Integer gid,Integer pageNo, Integer pageSize){
        if(pageNo==null){
            pageNo=1;
        }
        if(pageSize==null){
            pageSize=5;
        }
        //启动分页
        PageHelper.startPage(pageNo,pageSize);

        List<BusinessDetails> list=businessDetailsService.findDetailsByGid(gid);

        for(int i=0; i<list.size(); i++) {
            System.out.println(list.get(i).getBdid()+"\t"+list.get(i).getGid()+"\t"+list.get(i).getSid()
                    +"\t"+list.get(i).getAgnum()+"\t"+list.get(i).getOperation()+"\t"+list.get(i).getCreattime());
        }
        PageInfo<BusinessDetails> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }
    //情况2：通过sid值搜索
    @RequestMapping("findDetailsBySid")
    @ResponseBody
    public PageInfo<BusinessDetails> findDetailsBySid(Integer sid,Integer pageNo, Integer pageSize){
        if(pageNo==null){
            pageNo=1;
        }
        if(pageSize==null){
            pageSize=5;
        }
        //启动分页
        PageHelper.startPage(pageNo,pageSize);

        List<BusinessDetails> list=businessDetailsService.findDetailsBySid(sid);

        for(int i=0; i<list.size(); i++) {
            System.out.println(list.get(i).getBdid()+"\t"+list.get(i).getGid()+"\t"+list.get(i).getSid()
                    +"\t"+list.get(i).getAgnum()+"\t"+list.get(i).getOperation()+"\t"+list.get(i).getCreattime());
        }
        PageInfo<BusinessDetails> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }

    //情况3：通过gsid值搜索
    @RequestMapping("findDetailsByGsid")
    @ResponseBody
    public PageInfo<BusinessDetails> findDetailsByGsid(Integer gsid,Integer pageNo, Integer pageSize){
        if(pageNo==null){
            pageNo=1;
        }
        if(pageSize==null){
            pageSize=5;
        }
        //启动分页
        PageHelper.startPage(pageNo,pageSize);

        List<BusinessDetails> list=businessDetailsService.findDetailsByGsid(gsid);

        for(int i=0; i<list.size(); i++) {
            System.out.println(list.get(i).getBdid()+"\t"+list.get(i).getGid()+"\t"+list.get(i).getSid()
                    +"\t"+list.get(i).getAgnum()+"\t"+list.get(i).getOperation()+"\t"+list.get(i).getCreattime());
        }
        PageInfo<BusinessDetails> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }
    //情况4：通过gid, sid值搜索
    @RequestMapping("findDetailsByGidAndSid")
    @ResponseBody
    public PageInfo<BusinessDetails> findDetailsByGidAndSid(Integer gid,Integer sid,Integer pageNo, Integer pageSize){
        if(pageNo==null){
            pageNo=1;
        }
        if(pageSize==null){
            pageSize=5;
        }
        //启动分页
        PageHelper.startPage(pageNo,pageSize);

        List<BusinessDetails> list=businessDetailsService.findDetailsByGidAndSid(gid, sid);

        for(int i=0; i<list.size(); i++) {
            System.out.println(list.get(i).getBdid()+"\t"+list.get(i).getGid()+"\t"+list.get(i).getSid()
                    +"\t"+list.get(i).getAgnum()+"\t"+list.get(i).getOperation()+"\t"+list.get(i).getCreattime());
        }
        PageInfo<BusinessDetails> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }
    //情况5：通过gid, gsid值搜索
    @RequestMapping("findDetailsByGidAndGsid")
    @ResponseBody
    public PageInfo<BusinessDetails> findDetailsByGidAndGsid(Integer gid,Integer gsid,Integer pageNo, Integer pageSize){
        if(pageNo==null){
            pageNo=1;
        }
        if(pageSize==null){
            pageSize=5;
        }
        //启动分页
        PageHelper.startPage(pageNo,pageSize);

        List<BusinessDetails> list=businessDetailsService.findDetailsByGidAndGsid(gid, gsid);

        for(int i=0; i<list.size(); i++) {
            System.out.println(list.get(i).getBdid()+"\t"+list.get(i).getGid()+"\t"+list.get(i).getSid()
                    +"\t"+list.get(i).getAgnum()+"\t"+list.get(i).getOperation()+"\t"+list.get(i).getCreattime());
        }
        PageInfo<BusinessDetails> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }
    //情况6：通过sid, gsid值搜索
    @RequestMapping("findDetailsBySidAndGsid")
    @ResponseBody
    public PageInfo<BusinessDetails> findDetailsBySidAndGsid(Integer sid,Integer gsid,Integer pageNo, Integer pageSize){
        if(pageNo==null){
            pageNo=1;
        }
        if(pageSize==null){
            pageSize=5;
        }
        //启动分页
        PageHelper.startPage(pageNo,pageSize);

        List<BusinessDetails> list=businessDetailsService.findDetailsBySidAndGsid(sid, gsid);

        for(int i=0; i<list.size(); i++) {
            System.out.println(list.get(i).getBdid()+"\t"+list.get(i).getGid()+"\t"+list.get(i).getSid()
                    +"\t"+list.get(i).getAgnum()+"\t"+list.get(i).getOperation()+"\t"+list.get(i).getCreattime());
        }
        PageInfo<BusinessDetails> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }

    //删除功能
    @RequestMapping("delBusinessDetails")
    @ResponseBody
    public Map<Object, Object> delBusinessDetails(String ids) {
        System.out.println(ids);

        String idstr[] = ids.split(",");
        for (String str : idstr) {
            System.out.println(str);
        }
        System.out.println(idstr.length);

        Map<Object, Object> result = new HashMap<>();
        boolean flag = businessDetailsService.delBusinessDetails(idstr);
        if (flag) {
            result.put("status", "200");
            result.put("msg", "删除成功");
        } else {
            result.put("status", "500");
            result.put("msg", "删除失败");
        }
        return result;
    }

    @RequestMapping("addBusinessDetails")
    @ResponseBody
    public Map<Object, Object> addBusinessDetails(@RequestBody BusinessDetails businessDetails) {
        System.out.println(businessDetails.getBdid()+businessDetails.getAgnum()+businessDetails.getOperation());
        Map<Object, Object> result = new HashMap<>();
        boolean flag =businessDetailsService.addBusinessDetails(businessDetails);
        if (flag) {
            result.put("status", "200");
            result.put("msg", "添加成功");
        } else {
            result.put("status", "500");
            result.put("msg", "添加失败");
        }
        return result;
    }

    @RequestMapping("updateBusinessDetails")
    @ResponseBody
    public Map<Object, Object> updateBusinessDetails(@RequestBody BusinessDetails businessDetails) {
        System.out.println(businessDetails.getBdid()+businessDetails.getAgnum()+businessDetails.getOperation());
        Map<Object, Object> result = new HashMap<>();
        boolean flag =businessDetailsService.updateBusinessDetails(businessDetails);
        if (flag) {
            result.put("status", "200");
            result.put("msg", "添加成功");
        } else {
            result.put("status", "500");
            result.put("msg", "添加失败");
        }
        return result;
    }

}
